#!/usr/bin/env python3
"""
Real Progressive Indentation Analysis
Focuses on actual chapter files to detect systematic indentation issues
"""

import os
import re
from pathlib import Path

def analyze_real_files():
    """Analyze only the actual chapter files"""
    
    # Get all chapter files in proper order
    chapter_files = []
    for part_dir in sorted(Path('.').glob('part*')):
        if part_dir.is_dir():
            for ch_file in sorted(part_dir.glob('ch*.tex')):
                chapter_files.append(ch_file)
    
    print("=== REAL PROGRESSIVE INDENTATION ANALYSIS ===\n")
    
    progression_data = []
    
    for ch_file in chapter_files:
        try:
            with open(ch_file, 'r', encoding='utf-8') as f:
                content = f.read()
            
            # Analyze actual paragraph indentation
            lines = content.split('\n')
            
            # Track document structure elements
            section_commands = 0
            subsection_commands = 0
            subsubsection_commands = 0
            
            # Track actual content indentation (non-empty, non-comment lines)
            content_indentation = []
            max_content_indent = 0
            
            for line_num, line in enumerate(lines, 1):
                stripped = line.strip()
                if stripped and not stripped.startswith('%'):
                    # Count leading spaces for content lines
                    leading_spaces = len(line) - len(line.lstrip())
                    
                    # Skip LaTeX commands themselves, focus on content
                    if not (stripped.startswith('\\') or 
                           stripped.startswith('\begin') or 
                           stripped.startswith('\end')):
                        content_indentation.append(leading_spaces)
                        max_content_indent = max(max_content_indent, leading_spaces)
                    
                    # Count structure commands
                    if stripped.startswith('\\section{'):
                        section_commands += 1
                    elif stripped.startswith('\\subsection{'):
                        subsection_commands += 1
                    elif stripped.startswith('\\subsubsection{'):
                        subsubsection_commands += 1
            
            # Calculate average content indentation
            avg_content_indent = 0
            if content_indentation:
                avg_content_indent = sum(content_indentation) / len(content_indentation)
            
            # Extract part and chapter numbers
            part_match = re.search(r'part(\d+)', str(ch_file))
            ch_match = re.search(r'ch(\d+)', str(ch_file))
            
            part_num = int(part_match.group(1)) if part_match else 99
            ch_num = int(ch_match.group(1)) if ch_match else 99
            
            progression_data.append({
                'file': ch_file.name,
                'part': part_num,
                'chapter': ch_num,
                'max_indent': max_content_indent,
                'avg_indent': avg_content_indent,
                'sections': section_commands,
                'subsections': subsection_commands,
                'subsubsections': subsubsection_commands
            })
            
            print(f"{ch_file.name}:")
            print(f"  Part {part_num}, Chapter {ch_num}")
            print(f"  Max content indent: {max_content_indent} spaces")
            print(f"  Avg content indent: {avg_content_indent:.1f} spaces")
            print(f"  Structure: {section_commands} sections, {subsection_commands} subsections, {subsubsection_commands} subsubsections")
            print()
            
        except Exception as e:
            print(f"Error processing {ch_file}: {e}")
    
    # Analyze progression
    progression_data.sort(key=lambda x: (x['part'], x['chapter']))
    
    print("=== PROGRESSION SUMMARY ===")
    print("Part | Chapter | File | Max Indent | Avg Indent")
    print("-" * 60)
    
    for data in progression_data:
        print(f"{data['part']:4d} | {data['chapter']:7d} | {data['file'][:20]:20s} | {data['max_indent']:10d} | {data['avg_indent']:8.1f}")
    
    # Check for systematic increase
    if len(progression_data) > 5:
        early_files = progression_data[:len(progression_data)//3]
        late_files = progression_data[-len(progression_data)//3:]
        
        early_max = sum(f['max_indent'] for f in early_files) / len(early_files)
        late_max = sum(f['max_indent'] for f in late_files) / len(late_files)
        
        print(f"\n=== TREND ANALYSIS ===")
        print(f"Early files average max indent: {early_max:.1f} spaces")
        print(f"Late files average max indent: {late_max:.1f} spaces")
        
        if late_max > early_max + 1:
            print(f"⚠️  PROGRESSIVE INDENTATION DETECTED: +{late_max - early_max:.1f} spaces")
        else:
            print("✅ No significant progressive indentation detected")
    
    return progression_data

def check_document_structure():
    """Check for systematic structural issues"""
    
    print("\n=== DOCUMENT STRUCTURE ANALYSIS ===")
    
    # Check the main document for any indentation settings
    try:
        with open('gsi_technical_report.tex', 'r') as f:
            main_content = f.read()
        
        # Look for indentation-related settings
        indent_patterns = [
            r'\\setlength{\\parindent}{[^}]*}',
            r'\\setlength{\\parskip}{[^}]*}',
            r'\\addtolength{\\leftskip}{[^}]*}',
            r'\\addtolength{\\rightskip}{[^}]*}',
            r'\\leftskip\s*=',
            r'\\rightskip\s*='
        ]
        
        print("Indentation settings found:")
        for pattern in indent_patterns:
            matches = re.findall(pattern, main_content)
            if matches:
                for match in matches:
                    print(f"  {match}")
        
        # Check for any custom formatting commands
        custom_commands = re.findall(r'\\newcommand{[^}]*indent[^}]*}{[^}]*}', main_content, re.IGNORECASE)
        if custom_commands:
            print("Custom indentation commands:")
            for cmd in custom_commands:
                print(f"  {cmd}")
                
    except Exception as e:
        print(f"Error analyzing main document: {e}")

if __name__ == "__main__":
    data = analyze_real_files()
    check_document_structure()